<?php

namespace app\erp\controller;

use app\erp\model\Partnerdb;
use app\erp\model\Sitedb;
use app\erp\model\Goodsdb;
use app\erp\model\Categorydb;
use app\erp\model\Orderdb;
use app\erp\model\Memberdb;
use think\Cache;
use app\erp\model\Articledb;
use app\erp\model\Jdpricedb;
use think\Db;

class Index extends Base
{
    public $post = array();

    public function _initialize()
    {
        parent::_initialize();
        $request = request();
        $param = $request->param();
        $this->post = $param;
    }

    public function clearCache()
    {
        Cache::clear();
        $this->layuiReturn('清除缓存成功', 1, 'success', 1);
    }

    public function index()
    {
        if ($this->__system() === true) {
            if (PARTNER_ID) {
                $partner = new Partnerdb ();
                $get_partner = $partner->ajaxFind(PARTNER_ID);
                $this->assign('get_partner', $get_partner);
            }
            // 获取分站信息
            $sitedb = new Sitedb ();
            $get_site = $sitedb->ajaxFind(SITE_ID);
            $this->assign('get_site', $get_site);
            $this->assign('supplier_id', MEMBER_ID);
            return $this->fetch();
        }
    }


    public function count()
    {
        $articledb = new Articledb ();
        $articledb->param ['artclass_id'] = 11;
        $articledb->param ['page'] = 1;
        $articledb->param ['limit'] = 10;
        $articledb->param ['_o'] = 'article_id';
        $articledb->param ['_b'] = 'desc';
        $get_art = $articledb->ajaxList();
        $this->assign('get_art', $get_art);
        $jdpricedb = new Jdpricedb ();
        $get_data = $jdpricedb->ajaxList();
        $this->assign('jdprice', $get_data);
        return $this->fetch();
    }

    public function artdetail($article_id)
    {
        $articledb = new Articledb ();
        $articledb->param ['article_id'] = $article_id;
        $get_art = $articledb->ajaxFind();
        $this->assign('get_art', $get_art);
        return $this->fetch();
    }

    // 统计分站产品数据
    public function countSiteGoods()
    {
        // 获取分站
        $sitedb = new Sitedb ();
        $sitedb->param ['_o'] = 'site_id';
        $sitedb->param ['_b'] = 'asc';
        $sitedb->param ['page'] = 1;
        $sitedb->param ['limit'] = 100;
        $data = array();
        $all_num = 0;
        $goodsdb = new Goodsdb ();
        if (SITE_ID != null) {
            $sitedb->param ['site_id'] = SITE_ID;
        } else {
            $goodsdb->param ['site_id'] = '';
            $temp_count = $goodsdb->ajaxCount();
            $temp ['name'] = '主站';
            $temp ['value'] = $temp_count;
            $temp ['site_id'] = '';
            array_push($data, $temp);
            $all_num += $temp_count;
        }
        $get_site_list = $sitedb->ajaxSelect();

        foreach ($get_site_list ['data'] as $key => $val) {
            $goodsdb->param ['site_id'] = $val ['site_id'];
            $temp_count = $goodsdb->ajaxCount();
            $temp ['name'] = $val ['site_name'];
            $temp ['value'] = $temp_count;
            $temp ['site_id'] = $val ['site_id'];
            $temp ['type'] = 'site';
            array_push($data, $temp);
            $all_num += $temp_count;
        }
        $this->layuiReturn($data, $all_num, 'success', 1);
    }

    // 统计分类产品数据
    public function countCategoryGoods()
    {
        $site_id = isset ($this->post ['site_id']) ? $this->post ['site_id'] : null;
        $parent_sign = isset ($this->post ['parent_sign']) ? $this->post ['parent_sign'] : 0;
        $categorydb = new Categorydb ();
        $categorydb->param ['site_id'] = $site_id;
        $categorydb->param ['parent_sign'] = $parent_sign;
        $get_category_info = $categorydb->ajaxList();
        $data = array();
        $all_num = 0;
        $goodsdb = new Goodsdb ();
        $goodsdb->param ['site_id'] = $site_id;
        foreach ($get_category_info as $key => $val) {
            $goodsdb->param = array();
            if ($val ['level'] == 1) {
                $goodsdb->param ['category_sign1'] = $val ['category_sign'];
            } elseif ($val ['level'] == 2) {
                $goodsdb->param ['category_sign2'] = $val ['category_sign'];
            } else {
                $goodsdb->param ['category_sign3'] = $val ['category_sign'];
            }
            $temp_count = $goodsdb->ajaxCount();
            $temp ['name'] = $val ['category_name'];
            $temp ['value'] = $temp_count;
            $temp ['parent_sign'] = $val ['category_sign'];
            $temp ['type'] = 'category';
            $temp ['site_id'] = $site_id;
            $temp ['level'] = $val ['level'];
            array_push($data, $temp);
            $all_num += $temp_count;
        }
        $this->layuiReturn($data, $all_num, 'success', 1);
    }

    // 统计订单数量和订单金额
    public function countOrderNumAndMoney()
    {
        $result = [];
        $result['order_num'] = Db::name('order')->count();
        $result['order_all_money'] = Db::name('order')->sum('order_money');
        $this->layuiReturn($result);
    }

    // 统计分站订单
    public function countSiteOrder()
    {
        $type = $this->post ['type'];
        // 获取分站
        $sitedb = new Sitedb ();
        $sitedb->param ['_o'] = 'site_id';
        $sitedb->param ['_b'] = 'asc';
        $sitedb->param ['page'] = 1;
        $sitedb->param ['limit'] = 100;
        $data = array();
        $all_num = 0;
        $orderdb = new Orderdb ();
        if (SITE_ID != null) {
            $sitedb->param ['site_id'] = SITE_ID;
        } else {
            $orderdb->param ['site_id'] = '';
            if ($type == 'price')
                $temp_count = $orderdb->ajaxPriceCount() / 100;
            else
                $temp_count = $orderdb->ajaxCount();
            $temp ['name'] = '主站';
            $temp ['value'] = $temp_count;
            array_push($data, $temp);
            $all_num += $temp_count;
        }
        $get_site_list = $sitedb->ajaxSelect();

        foreach ($get_site_list ['data'] as $key => $val) {
            $orderdb->param ['site_id'] = $val ['site_id'];
            if ($type == 'price')
                $temp_count = $orderdb->ajaxPriceCount() / 100;
            else
                $temp_count = $orderdb->ajaxCount();
            $temp ['name'] = $val ['site_name'];
            $temp ['value'] = $temp_count;
            array_push($data, $temp);
            $all_num += $temp_count;
        }
        $this->layuiReturn($data, $all_num, 'success', 1);
    }

    // 统计会员
    public function countMember()
    {
        $count = Db::name('shopping')->group('phone')->count();
        /*$memberdb = new Memberdb ();
        $memberdb->param ['site_id'] = SITE_ID;
        $count = $memberdb->ajaxCount();*/
        $this->layuiReturn($count, $count, 'success', 1);
    }

    // 统计销量前20
    public function countGoodsTop20()
    {
        // 获取分站
        $sitedb = new Sitedb ();
        $sitedb->param ['_o'] = 'site_id';
        $sitedb->param ['_b'] = 'asc';
        $sitedb->param ['page'] = 1;
        $sitedb->param ['limit'] = 100;
        $data = array();
        $y_data = array();
        $goodsdb = new Goodsdb ();
        $goodsdb->param ['_o'] = 'sale_num';
        $goodsdb->param ['_b'] = 'desc';
        $goodsdb->param ['page'] = 1;
        $goodsdb->param ['limit'] = 20;
        if (SITE_ID != null) {
            $sitedb->param ['site_id'] = SITE_ID;
        } else {
            $goodsdb->param ['site_id'] = '';
            $data_goods = $goodsdb->ajaxList();
            $temp ['name'] = '主站';
            $temp ['type'] = 'bar';
            $temp ['data'] = array();
            foreach ($data_goods as $key => $val) {
                $t1 ['name'] = $val ['goods_name'];
                $t1 ['value'] = $val ['sale_num'];
                array_push($temp ['data'], $t1);
            }
            array_push($data, $temp);
        }
        $get_site_list = $sitedb->ajaxSelect();
        for ($i = 1; $i <= 20; $i++) {
            array_push($y_data, 'Top' . $i);
        }
        foreach ($get_site_list ['data'] as $k => $v) {
            $goodsdb->param ['site_id'] = $v ['site_id'];
            $data_goods = $goodsdb->ajaxList();
            $temp ['name'] = $v ['site_name'];
            $temp ['type'] = 'bar';
            $temp ['data'] = array();
            foreach ($data_goods as $key => $val) {
                $t1 ['name'] = $val ['goods_name'];
                $t1 ['value'] = $val ['sale_num'];
                array_push($temp ['data'], $t1);
                // array_push($y_data, $val['goods_sku']);
            }
            array_push($data, $temp);
        }
        $this->layuiReturn($data, 0, $y_data, 1);
    }

    public function countSiteOrderList()
    {
        // 获取分站
        $sitedb = new Sitedb ();
        $sitedb->param ['_o'] = 'site_id';
        $sitedb->param ['_b'] = 'asc';
        $sitedb->param ['page'] = 1;
        $sitedb->param ['limit'] = 100;
        $data = array();
        $all_num = 0;
        $all_price = 0;
        $xAxis = array();
        $legend = array();
        $new_site_arr = array();
        // 30天
        if ($this->post ['select_types'] == 'day') {
            $start_date = strtotime('-30 day');
            for ($i = 30; $i >= 0; $i--) {
                array_push($xAxis, date('m月d', strtotime('-' . $i . ' day')));
            }
        } else {
            $start_date = strtotime('-12 month');
            for ($i = 12; $i >= 0; $i--) {
                array_push($xAxis, date('y年m', strtotime('-' . $i . ' month')));
            }
        }
        if (SITE_ID != null) {
            $sitedb->param ['site_id'] = SITE_ID;
        } else {
            array_push($legend, '主站数量');
            array_push($legend, '主站金额');
            array_push($new_site_arr, array(
                'site_id' => '',
                'site_name' => '主站'
            ));
        }
        $get_site_list = $sitedb->ajaxSelect();
        foreach ($get_site_list ['data'] as $key => $val) {
            array_push($legend, $val ['site_name'] . '数量');
            array_push($legend, $val ['site_name'] . '金额');
            array_push($new_site_arr, array(
                'site_id' => $val ['site_id'],
                'site_name' => $val ['site_name']
            ));
        }
        $orderdb = new Orderdb ();
        foreach ($new_site_arr as $key => $val) {
            $temp_price ['name'] = $val ['site_name'] . '金额';
            $temp_price ['type'] = 'line';
            $temp_price ['yAxisIndex'] = 1;
            $temp_price ['data'] = array();
            $temp_num ['name'] = $val ['site_name'] . '数量';
            $temp_num ['type'] = 'bar';
            $temp_num ['data'] = array();
            $orderdb->param ['site_id'] = $val ['site_id'];
            if ($this->post ['select_types'] == 'day') {
                for ($i = 30; $i >= 0; $i--) {
                    $temp_date = strtotime('-' . $i . ' day');
                    $orderdb->param ['create_time_start'] = date('Y-m-d', $temp_date);
                    $orderdb->param ['create_time_end'] = date('Y-m-d', $temp_date);
                    $get_price_count = $orderdb->ajaxPriceCount() / 100;
                    array_push($temp_price ['data'], $get_price_count);
                    $get_num_count = $orderdb->ajaxCount();
                    array_push($temp_num ['data'], $get_num_count);
                }
            } else {
                for ($i = 12; $i >= 0; $i--) {
                    $temp_date = strtotime('-' . $i . ' month');
                    $orderdb->param ['create_time_start'] = date('Y-m', $temp_date) . '-01';
                    $orderdb->param ['create_time_end'] = date('Y-m', $temp_date) . '-31';
                    $get_price_count = $orderdb->ajaxPriceCount() / 100;
                    array_push($temp_price ['data'], $get_price_count);
                    $get_num_count = $orderdb->ajaxCount();
                    array_push($temp_num ['data'], $get_num_count);
                }
            }
            array_push($data, $temp_price);
            array_push($data, $temp_num);
        }
        $this->layuiReturn($data, $legend, $xAxis, 1);
    }

    // 统计年销量
    public function countYearOrderMoney()
    {
        $data = Db::name('order')->field('sum(order_money) all_money,FROM_UNIXTIME(create_time,"%Y") count_year')->group('count_year')->select();
        $xAxis = [];
        $series = [];
        foreach ($data as $key => $val) {
            if ($val['all_money']) {
                array_push($xAxis, $val['count_year']);
                array_push($series, ceil($val['all_money'] / 100000000) / 100);
            }
        }
        $this->layuiReturn($xAxis, $series);
    }

    // 统计用户活跃
    public function countUserActive()
    {
        $data = Db::name('shopping')->field('count(shopping_id) user_number,FROM_UNIXTIME(create_time,"%Y-%m") count_month')->group('count_month')->select();
        $xAxis = [];
        $series = [];
        foreach ($data as $key => $val) {
            array_push($xAxis, $val['count_month']);
            array_push($series, ceil($val['user_number'] / 10) / 100);
        }
        $this->layuiReturn($xAxis, $series);
    }

    // 上架商品统计
    public function countUpGoods()
    {
        $data = Db::name('baojia')->field('count(baojia_id) goods_number,FROM_UNIXTIME(create_time,"%Y-%m") count_month')->group('count_month')->select();
        $xAxis = [];
        $series = [];
        foreach ($data as $key => $val) {
            array_push($xAxis, $val['count_month']);
            array_push($series, ceil($val['goods_number'] / 100) / 100);
        }
        $this->layuiReturn($xAxis, $series);
    }

    // 分类销量占比
    public function countCateSale()
    {
        $top_category = Db::name('category')->where('site_id', 117)->where('level', 1)->field('category_name,category_sign')->where('api_option', '<>', '')->select();

        $all_cid = [];
        foreach ($top_category as $key => $val) {
            $two_category = Db::name('category')->where('parent_sign', $val['category_sign'])->column('category_sign');
            $three_category = Db::name('cart')->whereIn('category_sign', Db::name('category')->whereIn('parent_sign', $two_category)->column('category_sign'))->sum('goods_num');
            $temp['name'] = $val['category_name'];
            $temp['value'] = $three_category;
            array_push($all_cid, $temp);
        }
        $this->layuiReturn($all_cid);
    }

    /**
     * Title("发货超时订单")
     */
    public function deliverTimeout(){
        $data = Db::name('order')
            //->fetchSql(true)
            ->where('gys_fh_state',2)
            ->whereNull('delete_time')
            ->where('order_state','>',1)
            ->whereNotIn('order_type',[5,7])
            ->whereTime('create_time','<',strtotime('-3 day'))
            ->count();
        $this->layuiReturn($data);
    }

    /**
     * Title("收货超时")
     */
    public function receiveTimeOut(){
        $data = Db::name('order')
            ->where('gys_fh_state',1)
            ->where('gys_sh_state',2)
            ->whereNull('delete_time')
            ->where('order_state','>',1)
            ->whereNotIn('order_type',[5,7])
            ->whereTime('create_time','<',strtotime('-5 day'))
            ->count();
        $this->layuiReturn($data);
    }
}